package com.five.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.five.dao.backstage.Staff1Dao;
import com.five.util.LayTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 越川豪
 * @Date: 2020/11/27 14:21
 */
@RestController
@RequestMapping("Staff1Controller")
public class Staff1Controller {

    @Autowired
    private Staff1Dao staff1Dao;

    //查询个人信息
    @RequestMapping("find")
    public LayTable find(String name) {
        List list = staff1Dao.find(name);
        LayTable layTable = new LayTable(0, list);
        return layTable;
    }

    //修改地址
    @RequestMapping("update")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int result = staff1Dao.update(req.getParameter("address"), req.getParameter("name"));
        PrintWriter out = resp.getWriter();
        out.write(JSON.toJSONString(result));
    }

    //文件上传
    @RequestMapping("upload")
    public Map upload(MultipartFile file) {
        String path = "";
        Map map = new HashMap();
        //从文件名截取后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //获取随机码
        UUID uuid = UUID.randomUUID();
        //将随机码和后缀拼接，组成的新的文件名
        String objectName = uuid + suffix;
        try {
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            //oss-cn-beijing.aliyuncs.com
            String endpoint = "oss-cn-beijing.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
            String accessKeyId = "LTAI4GCzjhtjNLVfjx8BPm1h";
            String accessKeySecret = "dmXsWFjrdNanypvoLXYuDsuO56slhP";
            String bucketName = "yuyongli";

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);
            System.out.println(putObjectResult.toString());
            // 关闭OSSClient。
            ossClient.shutdown();

            path = "https://" + bucketName + "." + endpoint + "/" + objectName;
            //文件上传需要的参数  msg code data
            map.put("code", 0);
            map.put("msg", "");
            map.put("data", path);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    //上传头像
    @RequestMapping("refresh")
    public void refresh(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int result = staff1Dao.refresh(req.getParameter("path"), req.getParameter("name"));
        PrintWriter out = resp.getWriter();
        out.write(JSON.toJSONString(result));
    }
}
